package com.neardrug.services.impl;

import com.neardrug.entity.*;
import com.neardrug.mapper.*;
import com.neardrug.services.ApiResultService;
import com.neardrug.utils.ApiUtil;
import com.neardrug.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ApiResultServiceImpl implements ApiResultService {

    @Autowired
    private ApiResultMapper apiResultMapper;
    @Autowired
    private AnalysisMapper analysisMapper;
    @Autowired
    private ExamplesMapper examplesMapper;
    @Autowired
    private ImagesMapper imagesMapper;
    @Resource
    private FormImgMapper formImgMapper;
    @Resource
    private FormTableMapper formTableMapper;


    @Override
    public MarkdownResultVO getMarkdownByTaskId(Integer taskId) {
        Markdown markdown=apiResultMapper.getMarkdownByTaskId(taskId);
        if (markdown!=null){
            MarkdownResultVO markdownResultVO=new MarkdownResultVO();
            BeanUtils.copyProperties(markdown,markdownResultVO);
            return markdownResultVO;
        }else {
            Analysis analysis=analysisMapper.selectByTaskId(taskId);
            return MarkdownResultVO.builder()
                    .fileUrl(analysis.getFileUrl())
                    .build();
        }
    }

    @Override
    public RecordVO getRecordByTaskId(Integer taskId) {
        RecordVO recordVO=apiResultMapper.getRecordByTaskId(taskId);
        if (recordVO!=null){
            return recordVO;
        }else {
            Analysis analysis=analysisMapper.selectByTaskId(taskId);
            return RecordVO.builder()
                    .taskId(taskId)
                    .fileName(analysis.getFileName())
                    .fileUrl(analysis.getFileUrl())
                    .build();
        }
    }

    @Override
    public ExamplesVO getExamplesByTaskId(Integer taskId) {
        List<Synthesis> synthesisList=new ArrayList<>();//最后要返回的synthesis列表

        List<ExamplesInsert> examplesInsertList=examplesMapper.selectAll(taskId);
        if (examplesInsertList.size()!=0){//确实有数据
            //取出第一个数据项 用于获取taskId、fileName、fileUrl这些信息
            ExamplesInsert tempExamplesInsert = examplesInsertList.get(0);

            //将examplesInsertList封装为Synthesis的列表
            //遍历每一个examplesInsert项
            for (int i = 0; i < examplesInsertList.size(); i++) {
                ExamplesInsert examplesInsert = examplesInsertList.get(i);
                String exampleId = examplesInsert.getExampleId();//得到这个synthesis的exampleId
                Integer flag = ApiUtil.judgeIfExist(synthesisList, exampleId);
                if (flag==-1){//如果这个exampleId对应的合成反应还不存在 则应该需要创建一个
                    ChemicalCompound chemicalCompound=ChemicalCompound.builder()
                            .exampleId(examplesInsert.getExampleId())
                            .compoundId(examplesInsert.getCompoundId())
                            .intermediateName(examplesInsert.getIntermediateName())
                            .compoundName(examplesInsert.getCompoundName())
                            .smiles(examplesInsert.getSmiles())
                            .referenceReactions(examplesInsert.getReferenceReactions())
                            .HNMR(examplesInsert.getHNMR())
                            .CNMR(examplesInsert.getCNMR())
                            .FNMR(examplesInsert.getFNMR())
                            .PNMR(examplesInsert.getPNMR())
                            .MS(examplesInsert.getMS())
                            .IR(examplesInsert.getIR())
                            .UV(examplesInsert.getUV())
                            .Mp(examplesInsert.getMp())
                            .Rf(examplesInsert.getRf())
                            .AnalCalcd(examplesInsert.getAnalCalcd())
                            .LCMS(examplesInsert.getLCMS())
                            .HPLC(examplesInsert.getHPLC())
                            .EXAMPLELABEL(examplesInsert.getEXAMPLELABEL())
                            .REACTIONPRODUCT(examplesInsert.getREACTIONPRODUCT())
                            .STARINGMATERIAL(examplesInsert.getSTARINGMATERIAL())
                            .REAGENTCATALYST(examplesInsert.getREAGENTCATALYST())
                            .SOLVENT(examplesInsert.getSOLVENT())
                            .TIMES(examplesInsert.getTIMES())
                            .TEMPERATURE(examplesInsert.getTEMPERATURE())
                            .YIELDPERCENT(examplesInsert.getYIELDPERCENT())
                            .YIELDOTHER(examplesInsert.getYIELDOTHER())
                            .OTHERCOMPOUND(examplesInsert.getOTHERCOMPOUND())
                            .build();
                    List<SynthesisProcess> synthesisProcessList=new ArrayList<>();
                    SynthesisProcess synthesisProcess=SynthesisProcess.builder()
                            .stepId(examplesInsert.getStepId())
                            .stepCompound(examplesInsert.getStepCompound())
                            .stepDesc(examplesInsert.getStepDesc())
                            .proReferenceReactions(examplesInsert.getProReferenceReactions())
                            .proHNMR(examplesInsert.getProHNMR())
                            .proCNMR(examplesInsert.getProCNMR())
                            .proFNMR(examplesInsert.getProFNMR())
                            .proPNMR(examplesInsert.getProPNMR())
                            .proMS(examplesInsert.getProMS())
                            .proIR(examplesInsert.getProIR())
                            .proUV(examplesInsert.getProUV())
                            .proMp(examplesInsert.getProMp())
                            .proRf(examplesInsert.getProRf())
                            .proAnalCalcd(examplesInsert.getProAnalCalcd())
                            .proLCMS(examplesInsert.getProLCMS())
                            .proHPLC(examplesInsert.getProHPLC())
                            .proEXAMPLELABEL(examplesInsert.getProEXAMPLELABEL())
                            .proREACTIONPRODUCT(examplesInsert.getProREACTIONPRODUCT())
                            .proSTARINGMATERIAL(examplesInsert.getProSTARINGMATERIAL())
                            .proREAGENTCATALYST(examplesInsert.getProREAGENTCATALYST())
                            .proSOLVENT(examplesInsert.getProSOLVENT())
                            .proTIMES(examplesInsert.getProTIMES())
                            .proTEMPERATURE(examplesInsert.getProTEMPERATURE())
                            .proYIELDPERCENT(examplesInsert.getProYIELDPERCENT())
                            .proYIELDOTHER(examplesInsert.getProYIELDOTHER())
                            .proOTHERCOMPOUND(examplesInsert.getProOTHERCOMPOUND())
                            .build();
                    synthesisProcessList.add(synthesisProcess);
                    Synthesis synthesis=Synthesis.builder()
                            .chemicalCompound(chemicalCompound)
                            .synthesisProcessList(synthesisProcessList)
                            .build();
                    synthesisList.add(synthesis);
                }else {//如果这个exampleId对应的合成反应已经存在了
                    //获取到这个synthesis在synthesisList中的索引
                    Integer synthesisIndex = ApiUtil.getSynthesisByExampleId(synthesisList, exampleId);
                    Synthesis synthesis = synthesisList.get(synthesisIndex);
                    List<SynthesisProcess> synthesisProcessList = synthesis.getSynthesisProcessList();

                    SynthesisProcess synthesisProcess=SynthesisProcess.builder()
                            .stepId(examplesInsert.getStepId())
                            .stepCompound(examplesInsert.getStepCompound())
                            .stepDesc(examplesInsert.getStepDesc())
                            .proReferenceReactions(examplesInsert.getProReferenceReactions())
                            .proHNMR(examplesInsert.getProHNMR())
                            .proCNMR(examplesInsert.getProCNMR())
                            .proFNMR(examplesInsert.getProFNMR())
                            .proPNMR(examplesInsert.getProPNMR())
                            .proMS(examplesInsert.getProMS())
                            .proIR(examplesInsert.getProIR())
                            .proUV(examplesInsert.getProUV())
                            .proMp(examplesInsert.getProMp())
                            .proRf(examplesInsert.getProRf())
                            .proAnalCalcd(examplesInsert.getProAnalCalcd())
                            .proLCMS(examplesInsert.getProLCMS())
                            .proHPLC(examplesInsert.getProHPLC())
                            .proEXAMPLELABEL(examplesInsert.getProEXAMPLELABEL())
                            .proREACTIONPRODUCT(examplesInsert.getProREACTIONPRODUCT())
                            .proSTARINGMATERIAL(examplesInsert.getProSTARINGMATERIAL())
                            .proREAGENTCATALYST(examplesInsert.getProREAGENTCATALYST())
                            .proSOLVENT(examplesInsert.getProSOLVENT())
                            .proTIMES(examplesInsert.getProTIMES())
                            .proTEMPERATURE(examplesInsert.getProTEMPERATURE())
                            .proYIELDPERCENT(examplesInsert.getProYIELDPERCENT())
                            .proYIELDOTHER(examplesInsert.getProYIELDOTHER())
                            .proOTHERCOMPOUND(examplesInsert.getProOTHERCOMPOUND())
                            .build();
                    //将这个synthesisProcess加入synthesislist
                    synthesisProcessList.add(synthesisProcess);
                    //更新原来的synthesis
                    synthesis.setSynthesisProcessList(synthesisProcessList);
                    //更新synthesisList指定位置的synthesis值
                    synthesisList.set(synthesisIndex,synthesis);
                }
            }

            //返回VO对象
            return ExamplesVO.builder()
                    .taskId(taskId)
                    .fileName(tempExamplesInsert.getFileName())
                    .fileUrl(tempExamplesInsert.getFileUrl())
                    .synthesisList(synthesisList)
                    .build();
        }else {
            Analysis analysis=analysisMapper.selectByTaskId(taskId);
            return ExamplesVO.builder()
                    .taskId(taskId)
                    .fileName(analysis.getFileName())
                    .fileUrl(analysis.getFileUrl())
                    .build();
        }
    }

    @Override
    public ImagesVO getImagesByTaskId(Integer taskId) {
        List<Images> imagesList=imagesMapper.selectByTaskId(taskId);
        Analysis analysis = analysisMapper.selectByTaskId(taskId);
        return ImagesVO.builder()
                .taskId(analysis.getId())
                .fileName(analysis.getFileName())
                .fileUrl(analysis.getFileUrl())
                .imagesList(imagesList)
                .build();
    }

    @Override
    public FormVO getFormByTaskId(Integer taskId) {
        List<FormImg> formImgList = formImgMapper.getByTaskId(taskId);
        List<FormTable> formTableList = formTableMapper.getByTaskId(taskId);
        FormVO formVO=FormVO.builder()
                .taskId(taskId)
                .imgList(formImgList)
                .htmlList(formTableList)
                .build();
        return formVO;
    }
}
